// 登录操作
const btn = document.querySelector('.submit>button')
const nameInp = document.querySelector('.username')
const pwdInp = document.querySelector('.password')
const errBox = document.querySelector('.error')

// 1. 绑定事件
btn.addEventListener('click', e => {
  e = e || window.event
  try { e.preventDefault() } catch(err) { e.returnValue = false }

  const name = nameInp.value
  const pwd = pwdInp.value

  if (!name || !pwd) return window.alert('请完整填写表单')

  const data = `username=${ name }&password=${ pwd }`
  const xhr = new XMLHttpRequest()
  xhr.open('POST', 'http://localhost:9999/wcy_meizu/server/login.php')
  xhr.onload = function () {
    const res = JSON.parse(xhr.responseText)
    console.log(res)

    if (res.code === 0) {
      errBox.classList.add('active')
      return
    }
    if(res.code === 1){
      window.location.href = './index.html'
    }
    setcookie('info',res.info[0].username,1800)
    setcookie('id',res.info[0].Id,1800)
    setcookie('login',1,1800)  
}
  xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
  xhr.send(data)
})
